package com.example.androidstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.androidstudy.dao.daoImp.PersonDaoImp;
import com.example.androidstudy.model.Person;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private PersonDaoImp pdao;
    private String TAG = "cxp";
    private Button btn_add;
    private Button btn_query;
    private Button btn_all;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pdao = new PersonDaoImp(this);
        initView();
    }

    private void initView() {
//        pdao = new PersonDaoImp(this);
//        Person p = new Person(1,"xiaoming","xiaoming123");
//        pdao.add(p);
//        Person xiaoming = pdao.findById(1);
//        Log.e(TAG, "initView: "+xiaoming.getName() );
        btn_add = findViewById(R.id.btn_add);
        btn_query = findViewById(R.id.btn_query);
        btn_all = findViewById(R.id.btn_all);


        btn_add.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        btn_all.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
                Log.e(TAG, "onClick: " + v.getId() );
                Person p = new Person(1,"xiaoming","xiaoming123");
                pdao.add(p);

                break;
            case R.id.btn_query:
                Log.e(TAG, "onClick: " + v.getId() );
                Person xiaoming = pdao.findById(1);
                Log.e(TAG, "initView: "+xiaoming.toString() );
                break;
            case R.id.btn_all:
                Log.e(TAG, "onClick: " + v.getId() );
                List<Person> list = pdao.findAll();
                for(Person person : list){
                    Log.e(TAG, "onClick: " + person.toString());
                }
                break;
            default:
                break;
        }
    }
}